package main.SQL.objects;

import main.MainApp;
import main.SQL.MsSQLConnect;
import main.Trees.ProjectNode;

import java.sql.*;
import java.util.HashMap;

public class SQLDocumentStructure {

    private SQLDocument root;
    private HashMap<Integer, SQLDocument> docMap;
    private HashMap<Integer, SQLDocumentCategory> docCategoryMap;
    private HashMap<Integer, SQLDocumentStatus> docStatusMap;
    private HashMap<Integer, SQLDocumentAssignment> docAssignmentMap;
//    private HashMap<Integer, SQLDocument> docStatusMap;

    private Connection connSQL;

    public SQLDocumentStructure() {

        try {
            root = new SQLDocument();
            connSQL = MsSQLConnect.ConnectDB("Factage");
            fillMaps();
            connSQL.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    private void fillMaps() throws SQLException {

        Statement stmt = connSQL.createStatement();

        docCategoryMap = new HashMap<>();
        ResultSet rs = stmt.executeQuery("select * from [Factage].[dbo].[documentCategory] where db_name = '" + MainApp.curProject.getDbName() + "'" );
        while (rs.next()) {
            docCategoryMap.put(rs.getInt("catg_id"), new SQLDocumentCategory(rs));
        }

        docStatusMap = new HashMap<>();
        rs = stmt.executeQuery("select * from [Factage].[dbo].[documentStatusCodes] where db_name = '" + MainApp.curProject.getDbName() + "'" );
        while (rs.next()) {
            docStatusMap.put(rs.getInt("status_id"), new SQLDocumentStatus(rs));
        }

        docMap = new HashMap<>();
        rs = stmt.executeQuery("select * from [Factage].[dbo].[document] where proj_id = " + MainApp.curProject.getId() + " and db_name = '" + MainApp.curProject.getDbName() + "'" );
        while (rs.next()) {
            SQLDocument curDoc = new SQLDocument(rs);
            docMap.put(rs.getInt("doc_id"), curDoc);
            if (curDoc.getDocName().equals("Сметы Фактаж") && curDoc.getParentDocId() == 0) root = curDoc;
        }

        docAssignmentMap = new HashMap<>();
        rs = stmt.executeQuery("select * from [Factage].[dbo].[documentAssignment] where proj_id = " + MainApp.curProject.getId() + " and db_name = '" + MainApp.curProject.getDbName() + "'" );
        while (rs.next()) {
            SQLDocumentAssignment curDoc = new SQLDocumentAssignment(rs);
            docAssignmentMap.put(rs.getInt("id"), curDoc);
        }

        for (SQLDocument curDoc : docMap.values()) {
            if (curDoc.equals(root)) continue;
            if (docMap.containsKey(curDoc.getParentDocId())) docMap.get(curDoc.getParentDocId()).add(curDoc);
        }

        rs.close();
        stmt.close();
    }

    //region Блок геттеров и сеттеров
    public SQLDocument getRoot() {
        return root;
    }

    public void setRoot(SQLDocument root) {
        this.root = root;
    }
    //endregion


}
